//
//  MonthListViewController.swift
//  daily_count
//
//  Created by 段帅 on 2018/4/2.
//  Copyright © 2018年 Sooc. All rights reserved.
//

import UIKit
import MJRefresh
class MonthListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
   
    
    let blankView = DSToolManager.blank(string: "暂无记录");
    

    @IBOutlet weak var table: UITableView!
    var data = [[String:Any]]();
    var month = String();
    
    var page = 1;
    // 顶部刷新
    let header = MJRefreshNormalHeader();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //请求数据
        createData();
        //添加上拉加载  下拉刷新
        addMJRefresh();
    self.table.tableFooterView = UIView.init(frame: CGRect.init());
        
        self.table.addSubview(blankView);

        // Do any additional setup after loading the view.
    }
    
    func createData()  {
        if page == 1 {
            self.data.removeAll();
        }
        DSToolManager.postWithToken(URLString: severseUrl+api_getMonthList, parameters: ["page":page as AnyObject]) { (data_re, result) in
            if (result) {
                self.data = self.data + (data_re!["data"] as! [[String:Any]]);
                
                
               
                self.table.mj_header.endRefreshing();
                self.table.mj_footer.endRefreshing();
                if (self.data.count == (data_re!["total"] as! Int)){
                    self.table.mj_footer.endRefreshingWithNoMoreData();
                }
                
                if (self.page == 1 && self.data.count == 0){
                    self.blankView.isHidden = false;
                }else{
                    self.blankView.isHidden = true;
                }
                
                
                self.table.reloadData();
            }else {
                DSToolManager.alertFaild(message: data_re!["statusCode"] as! String);
            }
        }
    }

    
    
     func addMJRefresh(){
        self.table.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            print("下拉刷新.")
            self.page = 1;
            self.createData();
          
        });
        self.table.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: { 
            self.page+=1;
            self.createData();
        });
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "id");
        if (cell == nil) {
            cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: "id");
        }
        
        cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator;
        cell?.textLabel?.text = (data[indexPath.row]["month"] as? String)!+"月份账单";
        cell?.detailTextLabel?.text = String.init(format: "总花费：%@", (data[indexPath.row]["cost_all"] as? String)!);
        
        return  cell!;
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true);
        month = data[indexPath.row]["month"] as! String;
        self.performSegue(withIdentifier: "month_seg", sender: self);
 }
    //同步数据
    @IBAction func tongbuData(_ sender: Any) {
        DSToolManager.postWithToken(URLString: severseUrl+api_updateMonthList, parameters: nil) { (data_re, isSucess) in
            if (isSucess) {
                DSToolManager.alertSucess(message: "同步成功");
                self.page = 1;
                self .createData();
                
            }else{
                 DSToolManager.alertFaild(message: data_re!["statusCode"] as! String);
            }
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "month_seg" {
            let next = segue.destination as! DetailedViewController;
            next.month = month;
     
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
